package com.pwc.conversionTool.rest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.googlecode.objectify.condition.If;
import com.googlecode.objectify.condition.IfNotEmpty;
import com.pwc.conversionTool.biz.DataAdaptorBiz;
import com.pwc.conversionTool.entity.CsvDataType;
import com.pwc.conversionTool.entity.DataTemplate;
import com.pwc.conversionTool.entity.FilterCondition;
import com.pwc.conversionTool.entity.Formula;
import com.pwc.conversionTool.entity.FormulaCondition;

import static com.pwc.conversionTool.controller.sessionPersistence.AccountSessionPersistence.*;

@Controller
public class DataAdaptorRest {
	private static final Logger logger = LoggerFactory.getLogger(DataAdaptorRest.class);

	@Autowired
	public ServletContext context;

	DataAdaptorBiz dataAdaptorbiz = new DataAdaptorBiz();

	@RequestMapping(value = "/rest/dataAdaptor/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody DataTemplate saveDataAdaptor(@RequestBody DataTemplate dataAdaptor, HttpSession session) {

		try {

			dataAdaptor.setAccount(getCurrentAccount(session).getKey());
			
			DataTemplate result = dataAdaptorbiz.saveDataAdaptor(dataAdaptor);

			return result;
		} catch (Exception e) {
			
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));

			return null;

		}

	}
	
//	
//	/**
//	 * 
//	 * @param session
//	 * @return
//	 */
//	@RequestMapping(value = "/rest/dataAdaptor/getCurrentDataAdaptor", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
//	public @ResponseBody DataAdaptor getCurrentDataAdaptorRest( HttpSession session) {
//
//		try {
//			
//			return getCurrentDataAdaptor(session);
//			
//		} catch (Exception e) {
//			logger.error("log4j is running in rest/dataAdaptor/getCurrentDataAdaptor");
//			logger.error(Arrays.toString(e.getStackTrace()));
//			return null;
//		}
//
//	} 
	
	
	/**
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/rest/dataAdaptor/getDataAdaptorById", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody DataTemplate getDataAdaptorById(@RequestBody DataTemplate dataAdaptor, HttpSession session) {

		try {
			
			return dataAdaptorbiz.getDataAdaptorById(dataAdaptor.getId());
			
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
			return null;
		}

	} 
	
	
	
	
//	/**
//	 * 
//	 * @param session
//	 * @return
//	 */
//	@RequestMapping(value = "/rest/dataAdaptor/getCurrentCsvDataType", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
//	public @ResponseBody CsvDataType getCurrentCsvDataTypeRest( HttpSession session) {
//
//		try {
//			
//			DataAdaptor dataAdaptor= getCurrentDataAdaptor(session);
//			
//			if(dataAdaptor.getDataTypeKey()!=null)
//			{
//				return getCurrentCsvDataType(session);
//			}else {
//				return null;
//			}
//			
//			
//		} catch (Exception e) {
//			logger.error("log4j is running in rest/dataAdaptor/getCurrentCsvDataType");
//			logger.error(Arrays.toString(e.getStackTrace()));
//			return null;
//		}
//
//	} 
	
	/**
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/rest/dataAdaptor/getCsvDataTypeById", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody CsvDataType getCsvDataTypeById(@RequestBody CsvDataType csvDataType, HttpSession session) {

		try {
			CsvDataType result=null;
			//DataAdaptor dataAdaptor= dataAdaptorbiz.getCsvDataTypeById(id)
			if(csvDataType.getId()!=null&&csvDataType.getId()!=0)
			{
				result=dataAdaptorbiz.getCsvDataTypeById(csvDataType.getId());
			}
			
			return result;
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
			return null;
		}

	} 
	
	
	
	
//	@RequestMapping(value = "/rest/dataAdaptor/getIsEdit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
//	public @ResponseBody boolean getIsEditRest( HttpSession session) {
//
//		try {
//			
//			return getIsEdit(session);
//			
//		} catch (Exception e) {
//			logger.error("log4j is running in rest/dataAdaptor/getIsEdit");
//			logger.error(Arrays.toString(e.getStackTrace()));
//			return false;
//		}
//	} 
	
	
	
	
	
	

	@RequestMapping(value = "/rest/dataAdaptor/getAllDataAdaptor", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody List<DataTemplate> getAllDataAdaptor(HttpSession session) {		
		
		try {
			// may be add try catch here

			return dataAdaptorbiz.getDataAdaptorsByAccount(getCurrentAccount(session));
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));

			return null;
		}
	
	}

	@RequestMapping(value = "/rest/dataAdaptor/deleteDataAdaptor", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody boolean deleteDataAdaptor(@RequestBody DataTemplate dataTemplate, HttpSession session) {

		
		try {
			dataAdaptorbiz.deleteDataAdaptorById(dataTemplate.getId());

			return true;
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
			return false;
		}
	}
	
	
	
//	@RequestMapping(value = "/rest/dataAdaptor/showDetailSetting", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
//	public @ResponseBody boolean showDetailSetting(@RequestBody DataTemplate dataAdaptor, HttpSession session) {
//
//		
//		try {
//			DataTemplate dataAdaptor2= dataAdaptorbiz.getDataAdaptorById(dataAdaptor.getId());
//			saveCurrentDataAdaptor(session,dataAdaptor2);
//			
//			if (dataAdaptor2.getDataType().equalsIgnoreCase("CSV")) {
////				CsvDataType csvDataType=dataAdaptorbiz.getCsvDataTypeById(dataAdaptor2.getDataTypeKey().getId());
////				saveCurrentCsvDataType(session, csvDataType);
//			}
//			setIsEdit(session, false);
//			
//			return true;
//		} catch (Exception e) {
//			logger.error("log4j is running in rest/dataAdaptor/deleteDataAdaptor");
//			logger.error(Arrays.toString(e.getStackTrace()));
//			return false;
//		}
//	}
	
	
	
	@RequestMapping(value = "/rest/dataAdaptor/saveFormula", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody boolean saveFilter(@RequestBody Formula formula ,HttpServletRequest request, HttpSession session) {
		try {
			
			dataAdaptorbiz.saveFormula(formula);
			
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
			return false;
		}
	}
	
//	@RequestMapping(value = "/rest/dataAdaptor/getFilter", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
//	public @ResponseBody List<FilterCondition> getFilter(@RequestBody Long dateTemplateId ,HttpServletRequest request, HttpSession session) {
//		try {
//			
//			Formula formula=	dataAdaptorbiz.getFormula(dateTemplateId);
//			
//			return formula.getFilter();
//		} catch (Exception e) {
//			logger.error(e.getMessage());
//			logger.error(Arrays.toString(e.getStackTrace()));
//			return null;
//		}
//	}
	
	
	@RequestMapping(value = "/rest/dataAdaptor/getFormula", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Formula getFormula(@RequestBody Formula fomula ,HttpServletRequest request, HttpSession session) {
		try {
			
			Formula formula=dataAdaptorbiz.getFormula(fomula.getDataTemplateId());
			
			return formula;
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
			return null;
		}
	}
	

}
